package com.wbs.common;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.ComponentScan;

// spring 上下文取出相关的类
@ComponentScan("applicationContextHelper")
public class ApplicationContextHelper implements ApplicationContextAware {

    public static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        applicationContext=context;
    }

    public static <T> T popBean(Class<T> tClass){
        if(applicationContext==null){
            return null;
        }
        return applicationContext.getBean(tClass);  // 如果 applicationContext 不为空，直接取出 class
    }

    public static <T> T popBean(String name,Class<T> tClass){
        if(applicationContext==null){
            return null;
        }
        return applicationContext.getBean(name,tClass);
    }
}
